package org.haifi.game.service.impl;

import java.util.Map;

import org.haifi.game.GameContext;
import org.haifi.game.dao.PlayerMapper;
import org.haifi.game.model.MapInstance;
import org.haifi.game.model.Player;
import org.haifi.game.service.PlayerManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Maps;

@Component
public class PlayerManagerImpl extends BaseManagerImpl<Player> implements
		PlayerManager {
	/**
	 * 角色对象的内存缓存
	 */
	protected Map<Integer, Player> cache = Maps.newConcurrentMap();

	@Autowired
	private PlayerMapper playerMapper;

	@Override
	public Player get(int id) {
		Player player = cache.get(id);
		return player;
	}

	/**
	 * 玩家上线,并初始化上线数据
	 * 
	 * @param id
	 * @return
	 */
	public Player onlineInit(int id) {
		// TODO:这里需要添加保护,如果玩家还没有从内存中清除呢
		
		Player player=playerMapper.get(id);
		// 加入地图
		MapInstance mapInstance=GameContext.getBornMap();
		
		mapInstance.getPlayers().put(id, player);
		
		return player;
	}
}
